package de.unikoblenz.west.csxpoi.server;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.openrdf.model.URI;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;

/**
 * A collection of methods for creating new instances in the ontology.
 */
public class OntologyHelper {

	/**
	 * Generates an unused URI for a contribution instance.
	 * 
	 * @param connection
	 *            a repository connection
	 * @return an unused URI for a contribution instance
	 */
	public static String generateContributionUri(RepositoryConnection connection) {

		int maxId = 0;

		try {
			TupleQuery query = connection.prepareTupleQuery(
					QueryLanguage.SPARQL, "PREFIX base: <" + Constants.NS_BASE
							+ "> " + "SELECT ?contribution WHERE {"
							+ " ?contribution a base:Contribution ." + "}");
			TupleQueryResult result = query.evaluate();
			try {
				while (result.hasNext()) {
					BindingSet bindingSet = result.next();
					int id = Integer.valueOf(((URI) bindingSet
							.getValue("contribution")).getLocalName());
					if (id > maxId)
						maxId = id;
				}
			} finally {
				result.close();
			}
		} catch (RepositoryException e) {
			e.printStackTrace();
		} catch (MalformedQueryException e) {
			e.printStackTrace();
		} catch (QueryEvaluationException e) {
			e.printStackTrace();
		}

		return Constants.NS_CONTRIBUTION + (maxId + 1);
	}

	/**
	 * Generates an unused URI for a POI instance.
	 * 
	 * @param connection
	 *            a repository connection
	 * @return an unused URI for a POI instance
	 */
	public static String generatePoiUri(RepositoryConnection connection) {

		int maxId = 0;

		try {
			TupleQuery query = connection.prepareTupleQuery(
					QueryLanguage.SPARQL, "PREFIX base: <" + Constants.NS_BASE
							+ "> " + "SELECT ?poi WHERE {"
							+ " ?poi a base:Poi ." + "}");
			TupleQueryResult result = query.evaluate();
			try {
				while (result.hasNext()) {
					try {
						BindingSet bindingSet = result.next();

//						System.out.println("POIS: "
//								+ bindingSet.getValue("poi"));

						int id = Integer.valueOf(((URI) bindingSet
								.getValue("poi")).getLocalName());
						if (id > maxId)
							maxId = id;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} finally {
				result.close();
			}
		} catch (RepositoryException e) {
			e.printStackTrace();
		} catch (MalformedQueryException e) {
			e.printStackTrace();
		} catch (QueryEvaluationException e) {
			e.printStackTrace();
		}

		return Constants.NS_POI + (maxId + 1);
	}

	/**
	 * Generates a string of the current date and time formatted for
	 * xsd:dateTime.
	 * 
	 * @return the current date and time string
	 */
	public static String generateDateTimeString() {
		return generateDateTimeString(new Date());
	}

	/**
	 * Generates a string of the specified date and time formatted for
	 * xsd:dateTime.
	 * 
	 * @param dateTime
	 *            a date and time
	 * @return the specified date and time string
	 */
	public static String generateDateTimeString(Date dateTime) {
		String dateTimeString = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
				.format(dateTime);
		return new StringBuffer(dateTimeString).insert(
				dateTimeString.length() - 2, ":").toString();
	}

}
